package com.yricky.memoria.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import androidx.core.content.FileProvider
import com.yricky.memoria.MemoriaApp
import com.yricky.memoria.activity.PdfViewActivity
import com.yricky.memoria.activity.SectionActivity
import com.yricky.memoria.data.pojo.PdfQuery
import com.yricky.memoria.data.pojo.SectionQuery
import java.io.File

/**
 * @author Yricky
 * @date 2021/11/28
 */
val globalContext: MemoriaApp get() = MemoriaApp.inst

fun Context.startSectionActivity(section:SectionQuery){
    startActivity(Intent(
        this,
        SectionActivity::class.java
    ).apply {
        putExtra(
            "query",
            gson.toJson(section)
        ) }
    )
}

fun Context.startPdfActivity(pdfQuery: PdfQuery){

    startActivity(Intent(
        this,
        PdfViewActivity::class.java
    ).apply {
        putExtra(
            "query",
            gson.toJson(pdfQuery)
        ) }
    )
}

fun Context.openPdfExternal(pdfQuery: PdfQuery) {
    try{
        startActivity(Intent(Intent.ACTION_VIEW).apply {
            data = ws.getPdf(pdfQuery)?.pdfFile?.externalUri()
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        })
    }catch (e:Throwable){ }
}
fun File.externalUri():Uri{
    return FileProvider.getUriForFile(globalContext,"com.yricky.memoria.FileProvider",this)
}

fun File.captureVideoIntoItIntent():Intent{
    return Intent(MediaStore.ACTION_VIDEO_CAPTURE).also{
        it.putExtra(
            MediaStore.EXTRA_OUTPUT,
            this.externalUri()
        )
    }
}

fun File.captureImageIntoItIntent():Intent{
    return Intent(MediaStore.ACTION_IMAGE_CAPTURE).also{
        it.putExtra(
            MediaStore.EXTRA_OUTPUT,
            this.externalUri()
        )
    }
}